# settings ########################################################

mutfile = "lacI_deleterious.muts"
fileNameFormat = "del_{seq}_{iter}.sh"
mappoutFileFormat = "output/del_{seq}_{iter}.txt"
infile = "lacI.seq"
outdir = "../runscripts"
thisdir = "/storage/brno1/home/onpelikan/protein_test"

seqNums = [10 * i**2 for i in range(1,9)]
iterNums = range(1, 10)


# #################################################################
import os
import json
import re
# from utils import addDirToSysPath
# addDirToSysPath("../") #relative path to parent folder of mappanalysis package
import mappanalysis


def getSequenceFromFasta(fasta):
    return re.match(">.+\n(.+)", fasta).group(1)

print("Tests: " + str(len(seqNums) * len(iterNums)))
for seq in seqNums:
    for iterNum in iterNums:
        openedOutFile = open(os.path.join(outdir, fileNameFormat.format(seq=seq, iter=iterNum)), "w")

        #get sequence and mutations
        openedInFile = open(infile, "r")
        sequence = getSequenceFromFasta(openedInFile.read())
        openedInFile.close()
        openedInFile = open(mutfile, "r")
        mutations = mappanalysis.parseMutations(openedInFile.read())
        openedInFile.close()

        #create params
        params = {"filename": fileNameFormat.format(seq=seq, iter=iterNum),
                  "sequence" : sequence,
                  "mutations"    : mutations,
                  "num_alignments" : seq,
                  "num_iterations": iterNum}
        jsonparams = json.dumps(params)

        #generate openedInFile content
        openedOutFile.write("""
#!/bin/sh
. /packages/run/modules-2.0/init/sh
module add python-2.6.2
module add blast-2.2.26
module add clustal-omega-1.1.0
module add ics.muni.cz jdk-7
        """)
        openedOutFile.write("cd " + thisdir + "\n")
        openedOutFile.write("echo '" + jsonparams + "' | python run_protein_test.py > " + mappoutFileFormat.format(seq=seq, iter=iterNum)  + "\n")

        openedOutFile.write("\n")
        openedOutFile.close()



